<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <a-button @click="$refs.addOrUpdate.init('','add',formCode)" icon="plus" type="primary">新建</a-button>
    </div>
    <s-table
      bordered
      ref="table"
      :alert="alert"
      :data="loadData"
      :columns="columns"
      :rowKey="record => record.id"
    >
      <span slot="action" slot-scope="text,record">
        <span>
          <a href="javascript:;" @click="edit(record)">编辑</a>
          <a-divider type="vertical" />
        </span>
        <span>
          <a-popconfirm title="是否删除该条数据" @confirm="deleteItem(record)" okText="是" cancelText="否">
            <a-icon slot="icon" type="question-circle-o" style="color: red" />
            <a href="javascript:;" style="color: red">删除</a>
          </a-popconfirm>
        </span>
      </span>
    </s-table>
    <add-or-update
      @ok="queryPage"
      ref="addOrUpdate"
    />
  </a-card>

</template>

<script>

import { TableMixin } from '@/mixins/TableMixin'
import { fetchList, delObj } from '@/api/dynamicForm/data'
import { getObjByCode } from '@/api/dynamicForm/design'
import AddOrUpdate from './AddOrUpdate'
export default {
  name: 'Index',
  mixins: [TableMixin],
  components: {
    AddOrUpdate
  },
  data () {
    return {
      formCode: '',
      queryParam: {
      },
      columns: [],
      loadData: parameter => {
        return fetchList(
          this.formCode,
          Object.assign(parameter, this.queryParam)
        ).then(res => {
          return res.data
        })
      }
    }
  },
  methods: {
    edit (record) {
      this.$refs.addOrUpdate.init(record.id, 'edit', this.formCode)
    },
    deleteItem (record) {
      delObj(record.id).then(res => {
        this.$message.info('删除成功')
        this.queryPage()
      })
    }
  },
  created () {
    this.formCode = this.$route.params.formCode
    getObjByCode(this.formCode).then(res => {
      const record = JSON.parse(res.data.jsonData)
      this.columns = record.list.map(item => {
        return {
          title: item.label,
          dataIndex: item.model
        }
      })
      this.columns.push({
        title: '操作',
        scopedSlots: { customRender: 'action' },
        width: '200px'
      })
    })
  }
}
</script>

<style scoped>

</style>
